#include <bits\stdc++.h>
using namespace std;

int gcd(int a, int b)
{
	return b == 0 ? a : gcd(b, a % b);
}
int main()
{
	int t; cin >> t;
	while(t--)
	{
		int x, y; cin >> x >> y;
		int maxa = 0;
		for(int i = x + 1; i < y; i++)
		{
			maxa = max(maxa, (gcd(x, i) + gcd(i, y)));
		}
		cout << maxa << endl;
	}

}